package com.test.condition;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/11/27 18:46
 * <p>
 * 判断当前系统是否是Linux系统
 */
public class LinuxCondition implements Condition {

    /**
     * ConditionContext:判断条件能使用的上下文（环境）
     * AnnotatedTypeMetadata:注释信息
     *
     * @param context
     * @param metadata
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //1.能获取到ioc使用的beanfactory
        ConfigurableBeanFactory factory = context.getBeanFactory();
        //2.获取类加载器
        ClassLoader classloader = context.getClassLoader();
        //3.获取当前环境信息
        Environment environment = context.getEnvironment();
        //4.获取到bean定义的注册类,可以判断容器中的bean的注册情况
        BeanDefinitionRegistry beanDefinitionRegistry = context.getRegistry();

        String property = environment.getProperty("os.name");
        if (property.contains("Linux")) {
            return true;
        }
        return false;
    }
}
